package com.myblog.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myblog.Utils.BeanCopyUtils;

import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.CommentVo;
import com.myblog.domain.Vo.PageVo;
import com.myblog.domain.contants.GlobalConstants;
import com.myblog.domain.entity.Comment;
import com.myblog.services.CommentService;
import com.myblog.mapper.CommentMapper;
import com.myblog.services.UserService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【sg_comment(评论表)】的数据库操作Service实现
* @createDate 2022-09-27 17:38:07
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
implements CommentService{
    @Resource
    UserService userService;

    @Override
    public GlobalResponse getCommentList(String type,Long articleId, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Comment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(type.equals(GlobalConstants.IS_ARTICLE_COMMENT)){
            //是文章评论
            //根据articleId查询相关根评论
            lambdaQueryWrapper.eq(Comment::getArticleId,articleId);
            lambdaQueryWrapper.eq(Comment::getType,GlobalConstants.IS_ARTICLE_COMMENT);}
        else if(type.equals(GlobalConstants.IS_LINK_COMMENT)){
            //是友链评论
            lambdaQueryWrapper.eq(Comment::getType,GlobalConstants.IS_LINK_COMMENT);
        }
        //首先查询根评论
        lambdaQueryWrapper.eq(Comment::getRootId, GlobalConstants.IS_ROOT_COMMENT);
        //分页查
        Page<Comment> page=new Page(pageNum,pageSize);
        page(page, lambdaQueryWrapper);
        List<CommentVo> commentVoList=toCommentVo(page.getRecords());
        //用方法查子评论
         getChildren(commentVoList);
        //放在pageVo中
        PageVo pageVo=new PageVo(commentVoList,page.getTotal());
        return GlobalResponse.ok(pageVo);
    }

    @Override
    public GlobalResponse sendComment(Comment comment) {
        //一个是发过来的内容，一个是拿到登录信息放进去，还有自动设定创建时间-->交给mybatisPlus自动填充来做
        save(comment);
        return GlobalResponse.ok();
    }
    //Bean 拷贝然后通过查询
    private List<CommentVo> toCommentVo(List<Comment> commentList){
        List<CommentVo> commentVoList= BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
        commentVoList.stream()
                .peek(commentVo -> {
                    //根据createBy(id)查询username
                    commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getUserName());
                    //根据toCommentUserid查询toCommentUserName
                    Long userId=commentVo.getToCommentUserId();
                    if(userId!=-1) {commentVo.setToCommentUserName(userService.getById(userId).getUserName());}
                }).collect(Collectors.toList());
        return commentVoList;
    }

    private void getChildren(List<CommentVo> commentVoList){
         commentVoList.stream()
                .peek(commentVo -> {
                    LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
                    queryWrapper.eq(Comment::getRootId,commentVo.getId());
                    List<Comment> commentList = list(queryWrapper);
                    List<CommentVo> children = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
                    commentVo.setChildren(children);
                }).collect(Collectors.toList());
    }
}
